#include <jni.h>
#include <string>
#include"string.h"
extern "C"{
    #include "openssl/md5.h"
    #include "openssl/crypto.h"
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_itfitness_openssldemo_MainActivity_getMD5String(
        JNIEnv* env,
        jobject /* this */, jstring str) {
    const char *cStr = env->GetStringUTFChars(str, JNI_FALSE);
    unsigned char *data = (unsigned char *) cStr;
    unsigned char md[16];
    MD5(data,strlen(cStr),md);
    //必须进行如下操作否则会乱码
    int i;
    char tmp[3]={'\0'},buf[33]={'\0'};
    for (i = 0; i < 16; i++){
        sprintf(tmp,"%2.2x",md[i]);
        strcat(buf,tmp);
    }
    return env->NewStringUTF(buf);
}
extern "C"
JNIEXPORT jstring JNICALL
Java_com_itfitness_openssldemo_MainActivity_openSSLVersion(JNIEnv *env, jobject thiz) {
    return env->NewStringUTF(OpenSSL_version(OPENSSL_VERSION));
}